//加载用户农场数据----------------------------------------------------------------------
var currentPlayerId;
var current_player_farm;
//type :0-第一次加载；1-持续更新
function gamePlayerFarmInfo(type) {
	$.ajax({
		type: "POST",
		url: ajaxUrl + "/game/1.0/sfg/player/farm.js?jobId=" + getTimeMs(),
		data: {
			token: window.localStorage.getItem('token'),
			sid: currentSID,
			gameId: 'SFG'
		},
		dataType: "JSON",
		success: function(request) {
			console.log(request);
			if ((request.code == 0)||(request.code == 2)) {//正常开始游戏
				current_player_farm = request.data;
				currentPlayerId = current_player_farm.playerId;
				//刷新页面数据
				showPlayerFarm();
				if (type == 0) {//第一次加载
					doSystemMessage(request.msg);//提示信息
				} else {//页面刷新
					doSystemMessage('快看看有哪些变化吧......');
				}
				//加载土地数据
				setTimeout(gamePlayerFarmLandsDetail(),3000);
				///////////////////gamePlayerFarmLandsDetail();///////////////////////
			} else {
				doErrorMessage(request.code, request.msg);
			}
		},
		error: function(error) {
			console.log(error);
		}
	});
}
//加载用户庄园土地数据----------------------------------------------------------------------
var current_player_farm_lands_detail;
function gamePlayerFarmLandsDetail() {
	$.ajax({
		type: "POST",
		url: ajaxUrl + "/game/1.0/sfg/player/land.js?jobId=" + getTimeMs(),
		data: {
			token: window.localStorage.getItem('token'),
			sid: currentSID,
			gameId: 'SFG'
		},
		dataType: "JSON",
		success: function(request) {
			console.log(request);
			if (request.code != 0) {
				doErrorMessage(request.code, request.msg);
			} else {
				//数据整理为散列表
				current_player_farm_lands_detail = new HashTable();
				for (let i = 0; i < request.data.length; i++) {
					//console.log(request.data[i]);
					current_player_farm_lands_detail.put('land_' + i, request.data[i]);
				}
				////////////////////////界面初始化//////////////////////////
				//构建地图操作坐标-土地信息
				showPlayerFarmLandsDetail();
			}
		},
		error: function(error) {
			console.log(error);
		}
	});
}

////////////////////////////////////////////////////////////////////////////////
//构建玩家
function showPlayerFarm() {
	$('#player_name').html(current_player_farm.playerName);
	$('#player_money').html(current_player_farm.curMoney);
	$('#player_level').html(current_player_farm.farmLevel);
	$('#player_exp').attr("title", "当前经验值： " + current_player_farm.totalExp);
	$('#player_exp').css("width", (current_player_farm.totalExp / current_player_farm.nextExp).toFixed(2) * 100 + "%");
	$('#farm_exp').attr("title", "升级所需经验值： " + current_player_farm.nextExp);
	//$('#player_exp').css("width", (535/1000).toFixed(2)*100 + "%");
}
function showPlayerExp() {

}
//构建土地
function showPlayerFarmLandsDetail() {
	//初始化
	let dom = $('#mapBackgroundDiv');
	dom.html('');
	let str = '';
	//console.log(current_player_farm_lands_detail);
	for (let x = 0; x < 30; x++) {
		//console.log('land_==='+(x+1));
		var land = current_player_farm_lands_detail.get('land_' + x);
		//console.log('land==='+land);
		//下面逻辑迁移到后台实现
		if (checkEmpty(land)) {//判断是否有效
			break;
		} else {
			str = str + '<div class="mapbox" id="mapbox_' + land.landSort + '">';
			str = str + prepareLandView(land);
			str = str + '</div>';
		}
	}
	//console.log(str);
	dom.html(str);
}
//更新土地
function changePlayerFarmLandDetail(land, type) {
	//landSort,cropsName
	//初始化
	let dom = $('#mapbox_' + land.landSort);
	dom.html('');
	if (type == 10 || type == 40) {//第一次加载
		//dom.addClass('text-red');
	} else {
		//dom.removeClass('text-red');
	}
	
	if (type == 1) {//关闭
	} else if (type == 10) {//翻地
	} else if (type == 20) {//播种
	} else if (type == 30) {//收割
	} else if (type == 40) {//开垦
	} else if (type == 50) {//浇水
	} else if (type == 60) {//施肥
	} else if (type == 70) {//除草
	} else if (type == 80) {//杀虫
	}
	
	let str = prepareLandView(land);
	dom.html(str);
}

function prepareLandView(land){
//landState(1未开垦2未种植3未翻地)
	let str = '';
	if(land.landState<=1){
		str = str + '<div onclick="playerOperate(' + land.landState + ',\'' + land.landSort + '\')">' + 
			'<span class="span-block">待开垦</span>' + 
			'</div>';
	}
	else if (checkEmpty(land.cropsName)) {//判断是否有效
		str = str + '<div onclick="playerOperate(' + land.landState + ',\'' + land.landSort + '\')">' + 
			'<span class="span-block text-red">空闲中</span>' + 
			'</div>';
	} else {
		str = str + '<div onclick="playerOperate(' + land.landState + ',\'' + land.landSort + '\')">' + 
			'<span class="span-block">'+land.cropsName+'</span>' + 
			'<span class="span-block">'+land.stageName+'</span>' + 
			'<span class="span-block">'+land.needTime+'</span>' +
			'</div>';
	}
	
	return str;
}